import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName LC1046
 * @Author ykx
 * @Date 2024/7/10 19:21
 * @Version 1.0
 */
public class LC1046 {
    public static void main(String[] args) {
        LC1046 lc1046 = new LC1046();
        System.out.println(lc1046.lastStoneWeight(new int[]{2, 7, 4, 1, 8, 1}));
    }

    public int lastStoneWeight(int[] stones) {
        List<Integer> stoneList = new ArrayList<>();
        Arrays.sort(stones);
        for (int stone : stones) {
            stoneList.add(stone);
        }
        while (stoneList.size() > 1) {
            if (stoneList.get(stoneList.size() - 1).equals(stoneList.get(stoneList.size() - 2))) {
                stoneList.remove(stoneList.size() - 1);
                stoneList.remove(stoneList.size() - 1);
            } else {
                int weight = stoneList.get(stoneList.size() - 1) - stoneList.get(stoneList.size() - 2);
                stoneList.remove(stoneList.size() - 1);
                stoneList.remove(stoneList.size() - 1);
                stoneList.add(weight);
                Collections.sort(stoneList);
            }
        }
        return stoneList.isEmpty() ? 0 : stoneList.get(0);
    }
}
